package com.forever7776.life.web.config.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * 配置
 *
 * @author kz
 * @date 2018/03/28
 */
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

    /**
     * 最大空闲时间，设置24小时
     */
    private final static long MAX_SESSION_IDLE = 24 * 60 * 60 * 1000;

    private final static String interactiveScreen = "/ws/tweet_show";
    private final static String interactiveCtrl = "/ws/tweet_push";

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(interactiveScreenHandler(), interactiveScreen)
                .addInterceptors(new HandshakeInterceptor());
        registry.addHandler(interactiveCtrlHandler(), interactiveCtrl)
                .addInterceptors(new HandshakeInterceptor());
    }

    @Bean
    public WebSocketHandler interactiveScreenHandler() {
        return new InteractiveScreenHandler();
    }

    @Bean
    public WebSocketHandler interactiveCtrlHandler() {
        return new InteractiveCtrlHandler();
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(1024);
        container.setMaxBinaryMessageBufferSize(1024);
        container.setMaxSessionIdleTimeout(MAX_SESSION_IDLE);
        return container;
    }
}